package com.neuedu.jdbc.v1.dao;


import java.sql.*;
import java.util.List;
import java.util.Map;

public abstract class BaseDao {
    String url = "jdbc:mysql://127.0.0.1:3306/hrbu2021?useUnicode=true&serverTimezone=Asia/Shanghai&useSSL=false";
    String username = "root";
    String password = "root";
    private String driverClass = "com.mysql.cj.jdbc.Driver";

    /**
     * 通过的获取连接的方法
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName(driverClass);
        return DriverManager.getConnection(url, username, password);
    }

    //关闭资源的方法
    public void  close(ResultSet rs , Statement stmt, Connection conn){
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }


    //查询集合
    public abstract List<Map> selectList(String sql);

    //查询一个
    public abstract Map selectById(Integer id);

    //查询条数
    public abstract int selectCount(String sql);

    //更新(根据主键更新)
    public abstract int update(String sql ,Object ... params);

    //删除(根据主键删除)
    public abstract int deleteById(Integer id);



}
